package com.lxy.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxy.api.client.UserClient;
import com.lxy.common.constant.ResponseConstant;
import com.lxy.common.domain.common.R;
import com.lxy.common.domain.dto.MaintenanceUsersDTO;
import com.lxy.common.domain.vo.RepairUserVO;
import com.lxy.equipment.domain.po.MaintenanceUsers;
import com.lxy.equipment.mapper.MaintenanceUsersMapper;
import com.lxy.equipment.service.MaintenanceUsersService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
@RequiredArgsConstructor
public class MaintenanceUsersServiceImpl extends ServiceImpl<MaintenanceUsersMapper, MaintenanceUsers> implements MaintenanceUsersService {
    private final UserClient userClient;

    @Override
    public R arrangementMaintenanceSchedule(MaintenanceUsersDTO maintenanceUsersDTO) {
        //删除userIds包含的原有的user
        List<Long> userIds = maintenanceUsersDTO.getUserIds();
        LambdaQueryWrapper<MaintenanceUsers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(userIds!=null,MaintenanceUsers::getUserId,userIds);
        Long maintenanceId = maintenanceUsersDTO.getMaintenanceId();
        lambdaQueryWrapper.eq(MaintenanceUsers::getMaintenanceId,maintenanceId);
        remove(lambdaQueryWrapper);
        //封装数据
        List<MaintenanceUsers> maintenanceUsers=new ArrayList<>();
        for (Long userId : userIds) {
            MaintenanceUsers maintenanceUser=new MaintenanceUsers();
            maintenanceUser.setMaintenanceId(maintenanceId);
            maintenanceUser.setUserId(userId);
            maintenanceUsers.add(maintenanceUser);
        }
        //存储数据
        saveBatch(maintenanceUsers);
        return R.ok("分配每日维护任务成功");
    }

    @Override
    public R<List<RepairUserVO>> getRepairUsers(Long maintenanceId) {
        //获取这个维护订单的人员，传给user模块
        LambdaQueryWrapper<MaintenanceUsers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MaintenanceUsers::getMaintenanceId,maintenanceId);
        List<MaintenanceUsers> list = list(lambdaQueryWrapper);
        List<Long> userIds=new ArrayList<>();
        for (MaintenanceUsers maintenanceUsers : list) {
            userIds.add(maintenanceUsers.getUserId());
        }
        return userClient.getRepairUsers(userIds);
    }

    @Override
    public R deleteMaintenanceUsers(MaintenanceUsersDTO maintenanceUsersDTO) {
        LambdaQueryWrapper<MaintenanceUsers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(maintenanceUsersDTO.getMaintenanceId() != null,MaintenanceUsers::getMaintenanceId,maintenanceUsersDTO.getMaintenanceId());
        lambdaQueryWrapper.in(maintenanceUsersDTO.getUserIds() !=null,MaintenanceUsers::getUserId,maintenanceUsersDTO.getUserIds());
        boolean remove = remove(lambdaQueryWrapper);
        if(remove){
            return R.ok("删除维修人员成功");
        }else {
            return R.error(ResponseConstant.PARAMETER_ERROR);
        }
    }
}
